﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Drawing;
using System.Data;
using DevExpress.XtraGrid.Views.Grid;
using THSERP;
using THSLib;
using THSERP.BaseProvider;
using THSERP.BusinessEntities;
using System.Windows.Forms;
namespace THSERP.Modules.POS
{
    public partial class ICProductsGridControl : THSGridControl
    {
        public ICProductsGridControl()
        {
            InitializeComponent();
        }

        public ICProductsGridControl(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        public override void InitializeControl()
        {
            base.InitializeControl();
           // this.UseEmbeddedNavigator = true;
            //this.DoubleClick += new EventHandler(GridControl_DoubleClick);
            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.MainView;
            gridView.DoubleClick += new EventHandler(gridView_DoubleClick);
        }

        void gridView_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridview = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
            if (!((POSModule)Screen.Module).Toolbar.IsNullOrNoneAction())
            {

                //DevExpress.XtraGrid.GridControl gridControl = (DevExpress.XtraGrid.GridControl)sender;
                DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                if (gridView != null)
                {
                    POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
                    int rowHandle = gridView.FocusedRowHandle;
                    if (rowHandle >= 0)
                    {
                        ICProductsInfo objICProductsInfo = (ICProductsInfo)gridView.GetRow(rowHandle);
                        BRPOSItemsInfo objBRPOSItemsInfo = (BRPOSItemsInfo)entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName];
                        objBRPOSItemsInfo.BRPOSItemStatusCombo = BusinessObject.DefaultStatus;
                        if (!entity.BRPOSItemsList.Exists("FK_ICProductID", objICProductsInfo.ICProductID))
                        {
                            objBRPOSItemsInfo.BRPOSItemProductQty = 1;
                            objBRPOSItemsInfo.BRPOSItemQty1 = 0;
                            objBRPOSItemsInfo.BRPOSItemProductQtyOld = objBRPOSItemsInfo.BRPOSItemProductQty;
                            objBRPOSItemsInfo.FK_ICProductID = objICProductsInfo.ICProductID;
                            objBRPOSItemsInfo.FK_ICStockID = objICProductsInfo.FK_ICStockID;
                            objBRPOSItemsInfo.FK_ICProductGroupID = objICProductsInfo.FK_ICProductGroupID;
                            objBRPOSItemsInfo.FK_ICDepartmentID = objICProductsInfo.FK_ICDepartmentID;
                            objBRPOSItemsInfo.BRPOSItemStatusCombo = BusinessObject.DefaultStatus;
                        }
                        else
                        {
                            int iIndex = entity.BRPOSItemsList.PosOf("FK_ICProductID", objICProductsInfo.ICProductID);
                            entity.BRPOSItemsList.GridView.FocusedRowHandle = iIndex;

                            POSItemsEntity objCurrent = (POSItemsEntity)entity.BRPOSItemsList.GridView.GetRow(iIndex);
                            objCurrent.BRPOSItemProductQty += 1;
                            objCurrent.BRPOSItemProductQtyOld = objBRPOSItemsInfo.BRPOSItemProductQty;
                            objCurrent.FK_ICProductID = objICProductsInfo.ICProductID;
                            objCurrent.FK_ICStockID = objICProductsInfo.FK_ICStockID;
                            objCurrent.FK_ICProductGroupID = objICProductsInfo.FK_ICProductGroupID;
                            objCurrent.FK_ICDepartmentID = objICProductsInfo.FK_ICDepartmentID;
                            BRPOSItemsInfo newObject = (BRPOSItemsInfo)objCurrent.SetToBusinessObject("BRPOSItems");
                            entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName] = newObject;
                        }
                        entity.SetValuesAfterValidateProductQty();
                        ((POSModule)Screen.Module).ClikAddItemToPOSItemsList();
                    }
                }
            }
        }

       
        
        protected override DevExpress.XtraGrid.Views.Grid.GridView InitializeGridView()
        {
            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            if (gridView.Columns["FK_ICProductGroupID"] != null)
            {
                gridView.Columns["FK_ICProductGroupID"].VisibleIndex = 0;
                gridView.Columns["FK_ICProductGroupID"].Caption = " ";
                gridView.Columns["FK_ICProductGroupID"].Group();
            }

            if (gridView.Columns["ICProductName"] != null)
            {
                gridView.Columns["ICProductName"].VisibleIndex = 1;
                gridView.Columns["ICProductName"].Width = 150;
            }
            if (gridView.Columns["ICProductNo"] != null)
            {
                gridView.Columns["ICProductNo"].VisibleIndex = 2;
                gridView.Columns["ICProductNo"].AppearanceCell.BackColor = Color.WhiteSmoke;
            }
            gridView.OptionsSelection.EnableAppearanceFocusedRow = false;
            gridView.OptionsCustomization.AllowFilter = true;
            gridView.OptionsView.ShowAutoFilterRow = true;
            return gridView;
        }
        public override void InitGridControlDataSource()
        {
            //base.InitGridControlDataSource();
            POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            ICProductGroupsController objICProductGroupsController =new ICProductGroupsController();
            int iRiceID =objICProductGroupsController.GetObjectIDByName("Cơm văn phòng");
            int iBreakfastID =objICProductGroupsController.GetObjectIDByName("Điểm tâm");
            BindingSource bds = new BindingSource();
            DataSet ds = new ICProductsController().GetAllObjects();
            entity.lstICProducts.Clear();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                ICProductsInfo obj = (ICProductsInfo)new ICProductsController().GetObjectFromDataRow(row);
                if (obj != null)
                {
                    if (obj.ICProductTypeCombo.Equals(POSModule.cstProductTypeItem) || obj.ICProductTypeCombo.Equals(POSModule.cstProductTypeGoods) || obj.ICProductTypeCombo.Equals(POSModule.cstProductTypeFinished))
                    {
                        if (obj.FK_ICProductGroupID == iRiceID || obj.FK_ICProductGroupID == iBreakfastID)
                        {
                            continue;
                        }
                        else
                            entity.lstICProducts.Add(obj);
                    }
                }
            }
            bds.DataSource = entity.lstICProducts;
            this.DataSource = bds;
            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.MainView;
           // gridView.ExpandAllGroups();
        }

        #region GridControl Event Handlers
        private void GridControl_DoubleClick(object sender, EventArgs e)
        {
            if (!((POSModule)Screen.Module).Toolbar.IsNullOrNoneAction())
            {

                DevExpress.XtraGrid.GridControl gridControl = (DevExpress.XtraGrid.GridControl)sender;
                DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)gridControl.Views[0];
                if (gridView != null)
                {
                    POSEntities entity = (POSEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
                    int rowHandle = gridView.FocusedRowHandle;
                    if (rowHandle >= 0)
                    {
                        ICProductsInfo objICProductsInfo = (ICProductsInfo)gridView.GetRow(rowHandle);
                        BRPOSItemsInfo objBRPOSItemsInfo = (BRPOSItemsInfo)entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName];
                        if (!entity.BRPOSItemsList.Exists("FK_ICProductID", objICProductsInfo.ICProductID))
                        {
                            objBRPOSItemsInfo.BRPOSItemProductQty = 1;
                            objBRPOSItemsInfo.BRPOSItemProductQtyOld = objBRPOSItemsInfo.BRPOSItemProductQty;
                            objBRPOSItemsInfo.FK_ICProductID = objICProductsInfo.ICProductID;
                            objBRPOSItemsInfo.FK_ICStockID = objICProductsInfo.FK_ICStockID;
                            objBRPOSItemsInfo.FK_ICProductGroupID = objICProductsInfo.FK_ICProductGroupID;
                            objBRPOSItemsInfo.FK_ICDepartmentID = objICProductsInfo.FK_ICDepartmentID;
                        }
                        else
                        {
                            int iIndex = entity.BRPOSItemsList.PosOf("FK_ICProductID", objICProductsInfo.ICProductID);
                            entity.BRPOSItemsList.GridView.FocusedRowHandle = iIndex;

                            POSItemsEntity objCurrent = (POSItemsEntity)entity.BRPOSItemsList.GridView.GetRow(iIndex);
                            objCurrent.BRPOSItemProductQty += 1;
                            objCurrent.BRPOSItemProductQtyOld = objBRPOSItemsInfo.BRPOSItemProductQty;
                            objCurrent.FK_ICProductID = objICProductsInfo.ICProductID;
                            objCurrent.FK_ICStockID = objICProductsInfo.FK_ICStockID;
                            objCurrent.FK_ICProductGroupID = objICProductsInfo.FK_ICProductGroupID;
                            objCurrent.FK_ICDepartmentID = objICProductsInfo.FK_ICDepartmentID;
                            BRPOSItemsInfo newObject = (BRPOSItemsInfo)objCurrent.SetToBusinessObject("BRPOSItems");
                            entity.ModuleObjects[POSEntities.strBRPOSItemsObjectName] = newObject;
                        }
                        entity.SetValuesAfterValidateProductQty();
                        ((POSModule)Screen.Module).ClikAddItemToPOSItemsList();
                    }
                }
                //ProposeRemainingAmount();
            }
        }

       
        #endregion
       
    }
}
